First we are going to upload the libraries
library(tmap)
library(geojsonio)
library(sf)
library(raster)
library(tidyverse)
library(spData)
library(spatstat)
library(sp)
library(raster)
hunt <- geojson_read("https://www.dropbox.com/s/wa2ip35tcmt93g3/Team7.geojson?raw=1", what = "sp")
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(hunt) +
tm_lines (col = "green", lwd = 4)
st_as_sf ——st means is the generic of simple features, therefore we are transforming from sp to sf
hunt1 <- st_as_sf(hunt)
st_crs(hunt1)
## Coordinate Reference System:
## EPSG: 4326
## proj4string: "+proj=longlat +datum=WGS84 +no_defs"
we should take a look at the projection we also can ask to the head of our data simple features dont work well with rasters
head(hunt1)
## Simple feature collection with 1 feature and 11 fields
## geometry type: LINESTRING
## dimension: XY
## bbox: xmin: -0.1359 ymin: 51.47773 xmax: 0.033067 ymax: 51.54356
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## Name descriptio timestamp begin end altitudeMo
## 0 Casa team 7 2016 treasure hunt <NA> <NA> <NA> <NA>
## tessellate extrude visibility drawOrder icon
## 0 1 0 -1 <NA> <NA>
## geometry
## 0 LINESTRING (-0.1359 51.5221...
st_length(hunt1)
## 46610.68 m
hunt2 <- st_transform(hunt1, 27700)
st_crs
## function (x, ...)
## UseMethod("st_crs")
## <bytecode: 0x000000001cd44168>
## <environment: namespace:sf>
library(rgdal)
tubestations <- st_read("https://www.doogal.co.uk/LondonStationsKML.ashx","London stations with zone information")
## Reading layer `London stations with zone information' from data source `https://www.doogal.co.uk/LondonStationsKML.ashx' using driver `KML'
## Simple feature collection with 641 features and 2 fields
## geometry type: POINT
## dimension: XYZ
## bbox: xmin: -0.61143 ymin: 51.28216 xmax: 0.329952 ymax: 51.74702
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
stations1<- st_transform(tubestations, 27700)
head(stations1)
## Simple feature collection with 6 features and 2 fields
## geometry type: POINT
## dimension: XYZ
## bbox: xmin: 519457 ymin: 163744 xmax: 547297 ymax: 183352
## epsg (SRID): 27700
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
## Name
## 1 Abbey Road
## 2 Abbey Wood
## 3 Acton Central
## 4 Acton Main Line
## 5 Acton Town
## 6 Addington Village
## Description
## 1 <div>Zone 3</div> <div>E15 3NB</div> <div>Latitude: 51.531952</div> <div>Longitude: 0.003723</div> <div> <a href="https://www.doogal.co.uk/StationMap.php?station=Abbey Road">More info</a> </div>
## 2 <div>Zone 4</div> <div>SE2 9RH</div> <div>Latitude: 51.490784</div> <div>Longitude: 0.120272</div> <div> <a href="https://www.doogal.co.uk/StationMap.php?station=Abbey Wood">More info</a> </div>
## 3 <div>Zone 2</div> <div>W3 6BD</div> <div>Latitude: 51.508758</div> <div>Longitude: -0.26343</div> <div> <a href="https://www.doogal.co.uk/StationMap.php?station=Acton Central">More info</a> </div>
## 4 <div>Zone 3</div> <div>W3 0BP</div> <div>Latitude: 51.516887</div> <div>Longitude: -0.26769</div> <div> <a href="https://www.doogal.co.uk/StationMap.php?station=Acton Main Line">More info</a> </div>
## 5 <div>Zone 3</div> <div>W3 8HN</div> <div>Latitude: 51.503071</div> <div>Longitude: -0.280303</div> <div> <a href="https://www.doogal.co.uk/StationMap.php?station=Acton Town">More info</a> </div>
## 6 <div>Zone 3,4,5,6</div> <div>CR0 5AR</div> <div>Latitude: 51.356239</div> <div>Longitude: -0.032665</div> <div> <a href="https://www.doogal.co.uk/StationMap.php?station=Addington Village">More info</a> </div>
## geometry
## 1 POINT Z (539081 183352 -45....
## 2 POINT Z (547297 179002 -45....
## 3 POINT Z (520613 180299.1 -4...
## 4 POINT Z (520296 181196.1 -4...
## 5 POINT Z (519457 179639 -46....
## 6 POINT Z (537082 163744 -45....
tm_shape(stations1) +
tm_dots(col="blue")
### Next thing is the 100m distance buffer for the route.
hunt3 <- st_buffer(hunt2, dist = 100)
class(hunt2)
## [1] "sf" "data.frame"
tm_shape(hunt3) +
tm_polygons(col = "red")
### super buffer is done ### now we should plot the tube stations
library(tidyverse)
huntaddresses <- read.csv("https://www.dropbox.com/s/2cbu2ux9ddy9c0l/huntaddresses.csv?raw=1")
head(huntaddresses)
## Location Points lat lon
## 1 Picadilly Circus, London 2 51.51009 -0.1345676
## 2 Nelson's Column, London 2 51.50775 -0.1279302
## 3 Big Ben, London 2 51.50073 -0.1246254
## 4 100 Club, Oxford Street, London 5 51.51615 -0.1352761
## 5 Fabric, Charterhouse Street, London 5 51.51959 -0.1024735
## 6 The Gherkin, 30 St Mary Axe, London 2 51.51449 -0.0803065
## GoogleAddress
## 1 Piccadilly Circus, London W1D 7ET, UK
## 2 5 Trafalgar Square, London WC2N 5NJ, UK
## 3 Westminster, London SW1A 0AA, UK
## 4 100 Oxford St, Fitzrovia, London W1D 1LL, UK
## 5 77A Charterhouse St, Clerkenwell, London EC1M 6HJ, UK
## 6 30 St Mary Axe, London EC3A 8EP, UK
Harry Potter’s platform is giving a headache. the platform 9 3/4 has an address from the US let’s change it.
huntaddresses[29,3] <- 51.531427
huntaddresses[29,4] <- -0.1261330
huntaddresses1 <- st_as_sf(huntaddresses, coords = c("lon","lat"))
head
## standardGeneric for "head" defined from package "utils"
##
## function (x, ...)
## standardGeneric("head")
## <environment: 0x00000000223fd6b0>
## Methods may be defined for arguments: x
## Use showMethods("head") for currently available ones.
st_crs(huntaddresses1)
## Coordinate Reference System: NA
huntadresses2 <- st_set_crs(huntaddresses1, 4326)
st_crs(huntadresses2)
## Coordinate Reference System:
## EPSG: 4326
## proj4string: "+proj=longlat +datum=WGS84 +no_defs"
huntaddresses3 <- st_transform(huntadresses2, 27700)
head(huntaddresses3)
## Simple feature collection with 6 features and 3 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: 529485.2 ymin: 179643.9 xmax: 533303.9 ymax: 181780.4
## epsg (SRID): 27700
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
## Location Points
## 1 Picadilly Circus, London 2
## 2 Nelson's Column, London 2
## 3 Big Ben, London 2
## 4 100 Club, Oxford Street, London 5
## 5 Fabric, Charterhouse Street, London 5
## 6 The Gherkin, 30 St Mary Axe, London 2
## GoogleAddress
## 1 Piccadilly Circus, London W1D 7ET, UK
## 2 5 Trafalgar Square, London WC2N 5NJ, UK
## 3 Westminster, London SW1A 0AA, UK
## 4 100 Oxford St, Fitzrovia, London W1D 1LL, UK
## 5 77A Charterhouse St, Clerkenwell, London EC1M 6HJ, UK
## 6 30 St Mary Axe, London EC3A 8EP, UK
## geometry
## 1 POINT (529551.5 180667.4)
## 2 POINT (530018.7 180419)
## 3 POINT (530268.1 179643.9)
## 4 POINT (529485.2 181339.4)
## 5 POINT (531751.1 181780.4)
## 6 POINT (533303.9 181254.1)
tm_shape(huntaddresses3) +
tm_dots(col = "green")
findtubestations = st_intersection(hunt3,stations1)
## Warning: attribute variables are assumed to be spatially constant
## throughout all geometries
tm_shape(hunt3)+
tm_polygons(col='green')+
tm_shape(findtubestations)+
tm_dots(col="blue")
### This buffer is for question number 3
hunt4 <- st_buffer(hunt2, dist = 300)
tm_shape(hunt4) +
tm_polygons(col = "red")
### new intersection this intersection is for the question number 3
findhunts = st_intersection(hunt4, huntaddresses3)
## Warning: attribute variables are assumed to be spatially constant
## throughout all geometries
tm_shape(hunt4)+
tm_polygons(col = "blue")+
tm_shape(findhunts)+
tm_dots(col="red")
### clip the data to a single object ### Let’s go to the new question number 4 now we load the london wards shapefile.
library(rgdal)
LondonWards <- readOGR("London Wards/LondonWards.shp")
## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\Ricardo\Desktop\R Assessments\Week7 Assessment\London Wards\LondonWards.shp", layer: "LondonWards"
## with 625 features
## It has 77 fields
LondonWards1 <- st_as_sf(LondonWards)
LondonWards2<-st_transform(LondonWards1,27700)
londonWardsIntersect<- LondonWards2[hunt2,]
qtm(LondonWards2)
qtm(londonWardsIntersect)
tm_shape (londonWardsIntersect)+
tm_polygons(col = NA, alpha = 0.9)+
tm_shape(hunt) +
tm_lines (col = "green", lwd = 4)
londonWardsIntersect$MaleLE0509
## [1] 81.0 79.3 80.4 76.9 79.5 76.7 79.2 77.0 77.0 75.1 75.6 76.2 79.8 76.8
## [15] 78.6 78.9 74.8 77.8 75.0 78.0 76.2 75.3 80.9 78.9 76.9 76.3 74.8 81.8
## [29] 81.9 82.8
londonWardsIntersect$FemaleLE05
## [1] 84.8 88.8 86.1 82.4 82.1 84.6 88.2 83.1 81.0 81.9 86.9 82.4 81.7 85.2
## [15] 82.7 81.5 83.0 84.9 81.3 81.9 78.2 81.4 82.7 83.0 83.8 81.7 83.5 86.0
## [29] 85.8 86.2
londonwardsmax<-filter(londonWardsIntersect,MaleLE0509==max(londonWardsIntersect$MaleLE0509))
londonwardsmin<-filter(londonWardsIntersect,MaleLE0509==min(londonWardsIntersect$MaleLE0509))
print (londonwardsmax)
## Simple feature collection with 1 feature and 77 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 530967.7 ymin: 180406.7 xmax: 533839.6 ymax: 182206.1
## epsg (SRID): 27700
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
## WD11CD WD11CDO WD11NM WD11NMW WardName OldCode
## 1 E09000001 00AA City of London <NA> City of London 00AA
## Wardcode Pop2013 Aged0_15 Aged16_64 Aged65plus PctAged0_1 PctAged16_
## 1 E09000001 8000 600 6200 1200 7.5 77.5
## PctAged65p MeanAge201 MedianAge2 AreaSqKM PopDensity PctBame PctNotBorn
## 1 15 41.30389 39 3.15148 2538.062 21.4 36.7
## PctNoEngli GenFertRat MaleLE0509 FemaleLE05 PctYr1Obes PctYr6Obes
## 1 11.9 34 82.8 86.2 n/a n/a
## RateAmbula RatesAmbul RoadKilled InEmployme Employment NoJobs2011
## 1 811.5 18.34286 58 4747 75.26558 4e+05
## EmpWkAgePo RateNINoFo MeanHouseP NoProperti NoHousehol PctDetache
## 1 64.51613 138.8889 595000 195 5530 0.4
## PctSemiDet PctTerrace PctFlatMai PctOwned20 PctSocialR PctPrivate
## 1 0.2 1.4 98 42.3 16.5 35.9
## PctCTaxBan PctCTaxB_1 PctCTaxB_2 Incapacity IncomeSupp EmpSupport
## 1 4.1 63.1 32.8 0.8474576 0.5084746 1.355932
## JSAClaiman PctDepChil PctHHNoAdu PctLonePar IDRankLond IDPctWorst
## 1 1.943365 10 0.8665906 24.17582 551 20
## AvgGCSE201 UnauthAbse PctWithNoQ PctLev4Qua CrimeRate1 ViolenceRa
## 1 367 0.5 6.7 68.4 730.7895 68.02632
## RobberyRat TheftAndHa CriminalDa DrugsRate1 Deliberate PctOpenSpa
## 1 6.447368 445.1316 29.73684 65.52632 0.7792208 18.6425
## CarsPerHH2 AvgPubTran PctTTWBike TurnoutMay Shape_Leng Shape_Area ID
## 1 0.3858609 7.6 5.308616 48.52365 9601.643 3149381 625
## x y geometry
## 1 532462 181223 POLYGON ((532140.3 182191.9...
print (londonwardsmin)
## Simple feature collection with 2 features and 77 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 533478.3 ymin: 181723.6 xmax: 535646.4 ymax: 183144.6
## epsg (SRID): 27700
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
## WD11CD WD11CDO WD11NM WD11NMW
## 1 E05000574 00BGFX Bethnal Green South <NA>
## 2 E05000588 00BGGM Weavers <NA>
## WardName OldCode Wardcode Pop2013 Aged0_15
## 1 Tower Hamlets - Bethnal Green South 00BGFX E05000574 14700 2800
## 2 Tower Hamlets - Weavers 00BGGM E05000588 14150 2350
## Aged16_64 Aged65plus PctAged0_1 PctAged16_ PctAged65p MeanAge201
## 1 11200 750 19.0 76.2 5.1 30.2
## 2 10850 950 16.6 76.7 6.7 32.7
## MedianAge2 AreaSqKM PopDensity PctBame PctNotBorn PctNoEngli GenFertRat
## 1 28 0.9 16333.3 64.3 45.5 23.1 57
## 2 30 0.8 17687.5 47.9 41.2 16.7 46
## MaleLE0509 FemaleLE05 PctYr1Obes PctYr6Obes RateAmbula RatesAmbul
## 1 74.8 83.0 15.573770492 22.802850356 133.5374 1.105882
## 2 74.8 83.5 17.058823529 25.776397516 127.3498 1.155280
## RoadKilled InEmployme Employment NoJobs2011 EmpWkAgePo RateNINoFo
## 1 18 5978 54.12894 3500 0.3125000 90.26549
## 2 9 6754 64.66871 4300 0.3963134 74.57627
## MeanHouseP NoProperti NoHousehol PctDetache PctSemiDet PctTerrace
## 1 281000 89 5417 1.0 2.0 7.9
## 2 380000 118 5696 0.7 1.2 11.2
## PctFlatMai PctOwned20 PctSocialR PctPrivate PctCTaxBan PctCTaxB_1
## 1 89.0 21.2 44.9 31.5 31.05656 67.64497
## 2 86.9 24.3 43.4 30.3 32.33219 67.86684
## PctCTaxB_2 Incapacity IncomeSupp EmpSupport JSAClaiman PctDepChil
## 1 1.760939 2.142857 3.526786 4.776786 9.634944 38.93939
## 2 1.628370 2.073733 3.179724 4.654378 10.681299 39.07407
## PctHHNoAdu PctLonePar IDRankLond IDPctWorst AvgGCSE201 UnauthAbse
## 1 8.619375 71.08434 36 100 345 0.9
## 2 6.962139 62.98343 19 100 342 0.8
## PctWithNoQ PctLev4Qua CrimeRate1 ViolenceRa RobberyRat TheftAndHa
## 1 21.4 35.5 136.0 28.59107 8.041237 50.79038
## 2 20.6 43.9 172.2 25.79710 8.695652 77.17391
## CriminalDa DrugsRate1 Deliberate PctOpenSpa CarsPerHH2 AvgPubTran
## 1 9.896907 18.14433 1.2 16.8688 0.3371472 6.7
## 2 9.420290 32.10145 0.7 14.9729 0.3326754 5.0
## PctTTWBike TurnoutMay Shape_Leng Shape_Area ID x y
## 1 7.811977 35.62665 3858.472 881380.6 549 534921 182290
## 2 12.052117 39.77031 3589.704 778717.6 563 533973 182571
## geometry
## 1 POLYGON ((535646.4 182137.3...
## 2 POLYGON ((534355.7 182614.3...
londonwardsaveragemale<-group_by(londonWardsIntersect)%>%
summarise(mean_life = mean (MaleLE0509, na.rm = TRUE))
print(londonwardsaveragemale)
## Simple feature collection with 1 feature and 1 field
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 527890.8 ymin: 176208.9 xmax: 544649.4 ymax: 185624
## epsg (SRID): 27700
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
## # A tibble: 1 x 2
## mean_life geometry
## <dbl> <POLYGON [m]>
## 1 78.0 ((542176.5 180709, 542229.6 180707.9, 542437 180703.3, 542977~
londonwardsaveragefemale<-group_by(londonWardsIntersect)%>%
summarise(mean_life = mean (FemaleLE05, na.rm = TRUE))
print(londonwardsaveragefemale)
## Simple feature collection with 1 feature and 1 field
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 527890.8 ymin: 176208.9 xmax: 544649.4 ymax: 185624
## epsg (SRID): 27700
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
## # A tibble: 1 x 2
## mean_life geometry
## <dbl> <POLYGON [m]>
## 1 83.6 ((542176.5 180709, 542229.6 180707.9, 542437 180703.3, 542977~
superhunt <- huntaddresses3
score <- sf:::as_Spatial(superhunt)
window <- as.owin(LondonWards)
plot(window)
huntpoints <- score
huntpoints.ppp <- ppp(x=huntpoints@coords[,1],y=huntpoints@coords[,2],window=window)
## Warning: data contain duplicated points
plot (huntpoints.ppp,pch=16,cex=1,main="huntpoints")
#K test
K <- Kest(huntpoints.ppp, correction="border")
plot(K)